package cn.gok.springsecuritydemo05.config;


import cn.gok.springsecuritydemo05.filter.JWTTokenAuthencationFilter;
import cn.gok.springsecuritydemo05.filter.MySecurityMetadataSource;
import cn.gok.springsecuritydemo05.handler.LoginSuccessHandler;
import cn.gok.springsecuritydemo05.service.MyUserDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.ObjectPostProcessor;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.configurers.UrlAuthorizationConfigurer;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.access.intercept.FilterSecurityInterceptor;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
    基于RBAC的权限配置
 */
@Configuration
public class MySecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    MyUserDetailService userDetailService;

    @Autowired
    MySecurityMetadataSource securityMetadataSource;

    @Autowired
    LoginSuccessHandler loginSuccessHandler;

    @Autowired
    JWTTokenAuthencationFilter jwtTokenAuthencationFilter;


    @Override
    protected void configure(HttpSecurity http) throws Exception {

        //1、获取Spring容器
        ApplicationContext applicationContext = http.getSharedObject(ApplicationContext.class);

        http.apply(new UrlAuthorizationConfigurer<>(applicationContext))
                .withObjectPostProcessor(new ObjectPostProcessor<FilterSecurityInterceptor>() {
                    @Override
                    public <O extends FilterSecurityInterceptor> O postProcess(O object) {
                        //设置用自定义的权限验证元数据对象
                        object.setSecurityMetadataSource(securityMetadataSource);
                        object.setRejectPublicInvocations(true);
                        return object;
                    }
                });
        //将jwt token过滤器添加在UsernamePasswordAuthenticationFilter过滤器之前
        http.addFilterBefore(jwtTokenAuthencationFilter, UsernamePasswordAuthenticationFilter.class);

        http.formLogin()
                .successHandler(loginSuccessHandler)

                .and()
                .authorizeRequests()
                //任何请求都需要认证
                .anyRequest()
                .authenticated()
                .and()
                .sessionManagement()
                //禁止Security 从 session获取信息
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS);



        http.csrf().disable();
    }


    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailService);
    }
}
